package com.itheima.d02_threadpool_callable;

import java.util.concurrent.*;

/**
 * 目标：掌握线程池执行Callable任务
 */
public class Test {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //1.创建线程池对象
        ExecutorService pool = new ThreadPoolExecutor(
                2, //核心线程数
                5, //最大线程数
                3, //线程空闲时间
                TimeUnit.SECONDS, //时间单位
                new ArrayBlockingQueue<>(3), //任务队列
                Executors.defaultThreadFactory(), //线程工厂
                new ThreadPoolExecutor.CallerRunsPolicy() //直接拒绝
        );

        //2.执行任务
        Future<Integer> f1 = pool.submit(() -> {
            int sum = 0;
            for (int i = 0; i <= 10; i++) {
                sum += i;

            }
            return sum;
        });


        System.out.println("线程1执行结果："+f1.get());

        System.out.println("主线程执行over");

    }
}
